import {getCurrentPlatform} from "~/interface/Common";
import Axios from "axios";

const DEFAULT_GET_URL = '/spare-materials/list/'
const DEFAULT_POST_URL = '/spare-materials'
const DEFAULT_DELETE_URL = '/spare-materials/'
const DEFAULT_COUNT_URL = '/spare-materials/count/'
const QUERY_ID_URL = '/spare-materials/'
const DEFAULT_PUT_URL = '/spare-materials'
const SEARCH_URL = '/spare-materials/search/'
export async function getSpareMaterialManagementData(page,pageSize){
  const currentPlatform = await getCurrentPlatform.call(this)
  let result = null
  try{
    result = await Axios.get(DEFAULT_GET_URL + currentPlatform.unitId, {
      params: {
        page:page,
        pageSize:pageSize
      }
    })
  }catch(err){
    throw err
  }
  return result.data.parameter.list
}

export async function addSpareMaterial(form){
  const currentPlatform = await getCurrentPlatform.call(this)
  let result = null
  try{
    form.unitId = currentPlatform.unitId
    result = await Axios.post(DEFAULT_POST_URL, form)
  }catch(err){
    throw err
  }
  return result.data
}

export async function deleteSpareMaterial(id){
  let result = null
  try{
    result = await Axios.delete(DEFAULT_DELETE_URL + id)
  }catch (err){
    throw err
  }
  return result.data
}

export async function modifySpareMaterial(form){
  const currentPlatform = await getCurrentPlatform.call(this)
  let result = null
  try{
    form.unitId = currentPlatform.unitId
    result = await Axios.put(DEFAULT_PUT_URL, form)
  }catch(err){
    throw err
  }
  return result.data
}

export async function queryById(id){
  let result = null
  try{
    result = await Axios.get(QUERY_ID_URL + id)
  }catch (err){
    throw err
  }
  return result.data.parameter
}

export async function searchSpareMaterial(page,pageSize,searchBrand,searchModel){
  const currentPlatform = await getCurrentPlatform.call(this)
  let result = null
  try{
    result = await Axios.get(SEARCH_URL + currentPlatform.unitId, {
      params: {
        page: page,
        pageSize: pageSize,
        searchBrand:searchBrand,
        searchModel:searchModel
      }
    })
  }catch(err){
    throw err
  }
  console.log(result)
  return result.data.parameter.list
}

export async function count(){
  const currentPlatform = await getCurrentPlatform.call(this)
  let result = null
  try{
    result = await Axios.get(DEFAULT_COUNT_URL + currentPlatform.unitId)
  }catch(err){
    throw err
  }
  return result.data.parameter
}
